package com.kotlin.baselibrary.ext

import android.view.View
import android.widget.EditText
import com.kotlin.baselibrary.data.protocol.BaseResponse
import com.kotlin.baselibrary.rx.BaseFunction
import com.kotlin.baselibrary.rx.BaseFunctionBoolean
import com.kotlin.baselibrary.rx.BaseSubscriber
import com.trello.rxlifecycle3.LifecycleProvider
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers


/**
 * @author       zhipe
 * @createTime    2020/10/12
 * @description
 */

fun <T> Observable<T>.execute(subscriber: BaseSubscriber<T>, lifeCycleProvider: LifecycleProvider<*>) {
    this.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(lifeCycleProvider.bindToLifecycle())
            .subscribe(subscriber)
}

fun EditText.trimEditString(): String {
    return this.editableText.toString().trim()
}

fun View.onClick(onClickListener: View.OnClickListener) {
    this.setOnClickListener(onClickListener)
}

fun View.onClick(method: () -> Unit) {
    this.setOnClickListener { method() }
}

fun <T> Observable<BaseResponse<T>>.convert(): Observable<T> {
    return this.flatMap(BaseFunction())
}
fun <T> Observable<BaseResponse<T>>.convertBoolean(): Observable<Boolean> {
    return this.flatMap(BaseFunctionBoolean())
}
